package controller.member;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.class1.dao.Class1Dao;
import model.class1.dto.Class1Dto;
import model.member.dao.MemberDao;
import model.member.dto.FavorDto;
import model.member.dto.MemberDto;
import model.member.dto.ZipcodeDto;
import model.recipe.dao.RecipeDao;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import common.Constant;
import common.SearchDto;

public class MemberController extends MultiActionController{
	private String message;
	private Map<String,Object> map;	
	private MemberDto dto;
	private HttpSession session;
	private SearchDto search;
		
	private MemberDao memberDao;
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	private RecipeDao recipeDao;
	public void setRecipeDao(RecipeDao recipeDao) {
		this.recipeDao = recipeDao;
	}
	private Class1Dao class1Dao;
	public void setClass1Dao(Class1Dao class1Dao) {
		this.class1Dao = class1Dao;
	}
	////////////////////////////
	// 로그인
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response)throws Exception{
		dto = new MemberDto();
		dto.setUserid(request.getParameter("userid"));
		dto.setPasswd(request.getParameter("passwd"));
		dto = memberDao.login(dto);
		message = "아이디 혹은 비번이 일치하지 않습니다";
		if(dto != null ){
			session = request.getSession();
			session.setAttribute("userid", dto.getUserid());
			session.setAttribute("name", dto.getName());
			session.setAttribute("level", dto.getLevel());
			message = dto.getName()+"님 환영합니다!!!";			
		}	
		map = new HashMap<String, Object>();
		map.put("message", message);
		return new ModelAndView("/index.jsp","map",map);
	}
	// 로그아웃
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response)throws Exception{
		session = request.getSession();
		session.invalidate();
		map = new HashMap<String, Object>();
		map.put("message", "로그아웃되었습니다");
		return new ModelAndView("/index.jsp","map",map);
	}
	
	public ModelAndView useridCheck(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String userid = request.getParameter("userid");
		map = new HashMap<String, Object>();
		int result = memberDao.useridCheck(userid);
		String message = "사용이 불가능한 아이디 입니다";
		if( result == 0 ){
			message = "사용가능한 아이디 입니다";			
		}
		map.put("result", result);
		map.put("userid", userid);
		map.put("message", message);
		return new ModelAndView("/member/userid_check_result.jsp","map",map); 
	}	
	
	public ModelAndView zipcodeSearch(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String dong = request.getParameter("dong");
		List<ZipcodeDto> list = memberDao.zipcodeList(dong);
		map = new HashMap<String, Object>();
		map.put("list", list);		
		return new ModelAndView("/member/zipcode_search_result.jsp","map",map);
	}
	
	public ModelAndView join(HttpServletRequest request, HttpServletResponse response) throws Exception{
		MemberDto dto = new MemberDto();
		dto.setUserid(request.getParameter("userid"));
		dto.setPasswd(request.getParameter("passwd"));
		dto.setName(request.getParameter("name"));
		dto.setHp(request.getParameter("hp"));
		dto.setEmail(request.getParameter("email"));
		dto.setZipcode(request.getParameter("zipcode"));
		dto.setAddress1(request.getParameter("address1"));
		dto.setAddress2(request.getParameter("address2"));
		dto.setProfile(request.getParameter("profile"));
		// file upload
		MultipartRequest multi = (MultipartRequest)request;
		MultipartFile file = multi.getFile("file");
		String filename = file.getOriginalFilename();
		//long filesize = file1.getSize();
		if( !file.isEmpty()){			
			String path = Constant.path_member+dto.getUserid()+"\\";
			File f = new File(path+filename);
			if( !f.exists()){
				f.mkdirs(); // 디렉터리 생성	
			}
			file.transferTo(f);
			dto.setImage(filename);
		}
		// insert
		int row = memberDao.memberInsert(dto);
		map = new HashMap<String, Object>();
		if(row == 1){
			//map.put("message", "회원가입처리가 완료되었습니다");
			session = request.getSession();
			session.setAttribute("userid", dto.getUserid());
			session.setAttribute("name", dto.getName());
			session.setAttribute("level", 5);
		}
		return new ModelAndView("/index.jsp","map",map);
	}
	
	public ModelAndView memberList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		search = new SearchDto();
		search.setKeyword(request.getParameter("keyword"));
		search.setOp_level(request.getParameter("option_level"));
		search.setOp_rate(request.getParameter("option_rate"));
		search.setOp_limit(Integer.parseInt(request.getParameter("option_limit")));
		search.setOp_order(request.getParameter("option_order"));
		search.setOp_date(request.getParameter("option_date"));
		search.setOffset(Integer.parseInt(request.getParameter("offset")));			
		int count = memberDao.memberCount(search);
		List<MemberDto> list = memberDao.memberList(search);
		map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("count", count);
		return new ModelAndView("/member/member_result.jsp","map",map);
	}
	
	public void mb_level_update(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		if( session.getAttribute("level").toString().equals("0")){
			dto = new MemberDto();
			dto.setUserid(request.getParameter("userid"));
			dto.setLevel(Byte.parseByte(request.getParameter("level")));
			memberDao.mbLevelUpdate(dto);			
		}
	}
	
	public ModelAndView memberDetail(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userid = request.getParameter("userid");		
		map = new HashMap<String, Object>();
		map.put("dto", memberDao.memberDetail(userid));
		map.put("list_date",recipeDao.myListDate(userid));
		map.put("list_rate",recipeDao.myListRate(userid));		
		//map.put("list_class",classDao.myListDate(userid));
		return new ModelAndView("/member/member_detail.jsp","map",map);
	}
	
	public ModelAndView add_favor_member(HttpServletRequest request, HttpServletResponse response) throws Exception{
		map = new HashMap<String, Object>();
		session = request.getSession();
		dto = new MemberDto();
		dto.setUserid((String)session.getAttribute("userid"));
		dto.setFavorid(request.getParameter("favorid"));
		int result = memberDao.favoridCheck(dto);
		String message = "이미 추가된 회원입니다";
		if( result == 0	){
			memberDao.favoridInsert(dto);
			message = "정상적으로 추가되었습니다";
		}
		map.put("message", message);
		return new ModelAndView("/member/member_detail_result.jsp","map",map);
	}
	
	public void del_favor_member(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		String userid = (String)session.getAttribute("userid");
		FavorDto dto = new FavorDto();
		dto.setUserid(userid);
		dto.setFavorid(request.getParameter("favorid"));
		memberDao.favoridDelete(dto);
	}
	/////
	
	public void del_favor_recipe(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		String userid = (String)session.getAttribute("userid");
		FavorDto dto = new FavorDto();
		dto.setUserid(userid);
		dto.setIdx(Integer.parseInt(request.getParameter("idx")));
		memberDao.favorRecipeDelete(dto);
	}
	
	public ModelAndView myinfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		map = new HashMap<String, Object>();
		session = request.getSession();
		String userid = (String)session.getAttribute("userid");		
		
		map.put("myMemberList", memberDao.myMemberList(userid));
		map.put("myRecipeList", memberDao.myRecipeList(userid));
				
		
		return new ModelAndView("/member/member_detail_myinfo.jsp","map",map);
	}
	
	
	public ModelAndView my_resist_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		String userid = (String)session.getAttribute("userid");
		if( userid == null ){
			return new ModelAndView("redirect:/member/login.jsp");
		}
		search = new SearchDto();
		search.setUserid(userid);
		search.setOffset(Integer.parseInt(request.getParameter("offset")));
		search.setLimit(5);
		map = new HashMap<String, Object>();
		List<Class1Dto> list = class1Dao.myResistList(search);
		map.put("resistList", list);			
		return new ModelAndView("/member/myinfo_resist_list.jsp","map1",map);
	}
	
	public ModelAndView my_class_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		String userid = (String)session.getAttribute("userid");
		if( userid == null ){
			return new ModelAndView("redirect:/member/login.jsp");
		}
		search = new SearchDto();
		search.setUserid(userid);
		search.setOffset(Integer.parseInt(request.getParameter("offset")));
		search.setLimit(5);
		map = new HashMap<String, Object>();
		map.put("classList", class1Dao.myClassList(search));			
		return new ModelAndView("/member/myinfo_class_list.jsp","map2",map);
	}
	
	
	public ModelAndView add_favor_recipe(HttpServletRequest request, HttpServletResponse response) throws Exception{
		map = new HashMap<String, Object>();
		session = request.getSession();
		dto = new MemberDto();
		dto.setUserid((String)session.getAttribute("userid"));
		dto.setPoint(Integer.parseInt(request.getParameter("idx")));
		int result = memberDao.favorRecipeCheck(dto);
		String message = "이미 추가된 레시피입니다";
		if( result == 0	){
			memberDao.favorRecipeInsert(dto);
			message = "정상적으로 추가되었습니다";
		}
		map.put("message", message);
		return new ModelAndView("/member/member_detail_result.jsp","map",map);
	}
	
}
